package com.jsp.handlers;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class CountFileHandler {
	public static void writeFile(String filename, long count) {
		try {
			PrintWriter out = new PrintWriter(new FileWriter(filename));
			out.println(count);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("resource")
	public static long readFile(String filename) {
		File f = new File(filename);
		long count = 0;
		if (!f.exists()) {
			writeFile(filename, 0);
		}
		try {
			BufferedReader in = new BufferedReader(new FileReader(f));
			count = Long.parseLong(in.readLine());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return count;
	}
	public static String transform(long count){
		String countNumber=count+"";
		String newString="";
		for(int i=0;i<countNumber.length();i++){
			newString=newString+"<img src='images/"+countNumber.charAt(i)+".gif'>";
		}
//		System.out.println(newString);
		return newString;
	}
}
